﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

namespace YJKMenuAssist.MenuManageControls
{
	public partial class CenterList : UserControl
	{
		public CenterList()
		{
			this.InitializeComponent();
		}

		public List<string> GetlstCmdNameSelected()
		{
			return new List<string>();
		}

		public void RefreshContral(string tabName, CommandTableInfos commandTableInfos)
		{
			this.treeView.Items.Clear();
			for (int i = 0; i < commandTableInfos.TableInofList.Count; i++)
			{
				TableInfos tableInfos = commandTableInfos.TableInofList[i];
				if (tabName == tableInfos.TableName)
				{
					for (int j = 0; j < tableInfos.CommandInfoList.Count; j++)
					{
						if (tableInfos.CommandInfoList[j].CmdShow)
						{
							this.AddTreeNode(tableInfos.CommandInfoList[j]);
						}
					}
				}
			}
		}

		public void SearchKeywords(string cmdName, CommandTableInfos commandTableInfos)
		{
			this.treeView.Items.Clear();
			for (int i = 0; i < commandTableInfos.TableInofList.Count; i++)
			{
				TableInfos tableInfos = commandTableInfos.TableInofList[i];
				for (int j = 0; j < tableInfos.CommandInfoList.Count; j++)
				{
					if (tableInfos.CommandInfoList[j].CmdShow && tableInfos.CommandInfoList[j].CmdName.Contains(cmdName) && string.Empty != cmdName)
					{
						this.AddTreeNode(tableInfos.CommandInfoList[j]);
					}
				}
			}
		}

		private void AddTreeNode(CommandInfos commandInfos)
		{
			TreeViewItem treeViewItem = new TreeViewItem();
			treeViewItem.Header = commandInfos.CmdName;
			if (commandInfos.LstCommandInfos.Count == 0)
			{
				treeViewItem.Tag = commandInfos;
			}
			else
			{
				foreach (CommandInfos commandInfos2 in commandInfos.LstCommandInfos)
				{
					TreeViewItem treeViewItem2 = new TreeViewItem();
					treeViewItem2.Header = commandInfos2.CmdName;
					treeViewItem2.Tag = commandInfos2;
					if (commandInfos2.CmdShow)
					{
						treeViewItem.Items.Add(treeViewItem2);
					}
				}
			}
			this.treeView.Items.Add(treeViewItem);
		}

		private void treeView_PreviewMouseMove(object sender, MouseEventArgs e)
		{
			if (Mouse.LeftButton != MouseButtonState.Pressed)
			{
				return;
			}
            System.Windows.Point position = e.GetPosition(this.treeView);
			if (VisualTreeHelper.HitTest(this.treeView, position) == null)
			{
				return;
			}
			if (this.treeView.SelectedItem is TreeViewItem && e.OriginalSource is TextBlock)
			{
				DataObject data = new DataObject("myFormat", this.treeView.SelectedItem as TreeViewItem);
				DragDrop.DoDragDrop(this.treeView.SelectedItem as TreeViewItem, data, DragDropEffects.Move);
			}
		}

		private void treeView_Drop(object sender, DragEventArgs e)
		{
			try
			{
				DependencyObject parent = (((Grid)(e.Source as TextBlock).Parent).Parent as TreeViewItem).Parent;
			}
			catch
			{
			}
		}

		private List<string> m_lstStrCmdName = new List<string>();
	}
}
